package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.AnalysisWebsiteParam;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.AnalysisWebsiteParamDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2018-11-26 15:07:22
 */
public class AnalysisWebsiteParamConverts {

    private static Logger LOG = LoggerFactory.getLogger(AnalysisWebsiteParamConverts.class);

    public static List<AnalysisWebsiteParamDTO> convert(List<AnalysisWebsiteParam> analysisWebsiteParamList) {
        if (CollectionUtils.isEmpty(analysisWebsiteParamList)) {
            return Collections.EMPTY_LIST;
        }

        List<AnalysisWebsiteParamDTO> analysisWebsiteParamListResult = Lists.newArrayList();
        for (AnalysisWebsiteParam analysisWebsiteParam : analysisWebsiteParamList) {
            analysisWebsiteParamListResult.add(convert(analysisWebsiteParam));
        }
        return analysisWebsiteParamListResult;
    }

    public static AnalysisWebsiteParamDTO convert(AnalysisWebsiteParam analysisWebsiteParam) {
        try {
            return BeanUtils.copy(analysisWebsiteParam, AnalysisWebsiteParamDTO.class);
        } catch (Exception e) {
            LOG.error("AnalysisWebsiteParam到AnalysisWebsiteParamDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisWebsiteParam到AnalysisWebsiteParamDTO转换失败", e);
        }
    }

    public static AnalysisWebsiteParam convert(AnalysisWebsiteParamDTO analysisWebsiteParamDTO) {
        try {
            return BeanUtils.copy(analysisWebsiteParamDTO, AnalysisWebsiteParam.class);
        } catch (Exception e) {
            LOG.error("AnalysisWebsiteParamDTO到AnalysisWebsiteParam转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisWebsiteParamDTO到AnalysisWebsiteParam转换失败", e);
        }
    }
}